package top.suven.base.http.admin.sec.controller;

import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.sec.model.RoleMenu;
import top.suven.base.http.admin.sec.service.RoleMenuService;
import top.suven.base.http.admin.sec.vo.request.RoleMenuRequestVo;
import top.suven.base.http.admin.sec.vo.response.RoleMenuResponseVo;
import top.suven.base.http.admin.sec.vo.valid.SecValidGroup;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**   
 * @Title: RoleMenuController.java
 * @Description: 角色菜单权限的控制服务类
 * @author lixiangling
 * @date   2018/08/13 16:59:19
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/sec/roleMenu")
public class RoleMenuController {
    private final static Logger logger = LoggerFactory.getLogger(RoleMenuController.class);
	@Autowired
	private RoleMenuService roleMenuService;
    /**
     * @Title: 跳转到角色菜单权限主界面
     * @Description:roleMenuRequestVo @{Link RoleMenuRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:59:19
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    public String index(){
        return "sec/roleMenu_index";
    }
    /**
     * @Title: 获取角色菜单权限分页信息
     * @Description:roleMenuRequestVo @{Link RoleMenuRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:59:19
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public PageData<RoleMenuResponseVo> list(@Validated(value = {SecValidGroup.SecQueryGroup.class}) RoleMenuRequestVo roleMenuRequestVo){
        RoleMenu roleMenu = new RoleMenu();
        BeanUtil.copyProperties(roleMenuRequestVo, roleMenu);
        return roleMenuService.getRoleMenuPage(roleMenu, roleMenuRequestVo.getPage(), roleMenuRequestVo.getLimit());
    }
   /**
    * @Title: 保存角色菜单权限信息
    * @Description:roleMenuRequestVo @{Link RoleMenuRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:59:19
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "save")
    public ResponseResult save(@Validated(value = {SecValidGroup.SecFormGroup.class}) RoleMenuRequestVo roleMenuRequestVo){
        RoleMenu roleMenu = new RoleMenu();
        BeanUtil.copyProperties(roleMenuRequestVo, roleMenu);
        if(roleMenu.getRoleMenuId() != null){
            roleMenuService.updRoleMenu(roleMenu);
        }else{
            roleMenu.setStatus(StatusEnum.VALID.getIndex());
            roleMenu.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
            roleMenuService.addRoleMenu(roleMenu);
        }
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看角色菜单权限信息
    * @Description:roleMenuRequestVo @{Link RoleMenuRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:59:19
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    public String detail(ModelMap modelMap, Long id){
        RoleMenu roleMenu = null;
        if(id > 0){
            roleMenu = roleMenuService.getRoleMenu(id);
        }
        modelMap.put("roleMenu", roleMenu);
        return "sec/roleMenu_detail";
    }
    /**
     * @Title: 跳转角色菜单权限编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:59:19
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    public String edit(ModelMap modelMap, @RequestParam(defaultValue = "0") Long id){
        RoleMenu roleMenu = null;
        if(id > 0){
            roleMenu = roleMenuService.getRoleMenu(id);
        }
        modelMap.put("roleMenu", roleMenu);
        return "sec/roleMenu_edit";
    }
    /**
     * @Title: 删除角色菜单权限信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:59:19
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    public ResponseResult del( @RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        roleMenuService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用角色菜单权限信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:59:19
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        roleMenuService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用角色菜单权限信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:59:19
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        roleMenuService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出角色菜单权限信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:59:19
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    public void export(HttpServletResponse response, RoleMenuRequestVo roleMenuRequestVo){
        RoleMenu roleMenu = new RoleMenu();
        BeanUtil.copyProperties(roleMenuRequestVo, roleMenu);
        PageData<RoleMenuResponseVo> roleMenuPage = roleMenuService.getRoleMenuPage(roleMenu, roleMenuRequestVo.getPage(), roleMenuRequestVo.getLimit());
        List<RoleMenuResponseVo> data = roleMenuPage.getData();

        ColumnBox columnBox = ColumnBox.create()
                
			.add("roleMenuId", "角色菜单ID")
			.add("roleCode", "角色编码")
			.add("menuCode", "菜单编码")
			.add("status", "状态")
			.add("reorder", "排序")
			.add("remark", "备注")
			.add("enableStatus", "启用状态 启用-1、禁用-0");

        try {
            ExportUtils.write(String.format("角色菜单权限_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}